package cn.fafu.fafu_record.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class HttpUtil {

    public static String BOUNDARY = UUID.randomUUID().toString();
    public static String boundaryPrefix = "--";
    public static String newLine = "\r\n";
    private static HttpURLConnection conn;
    private static BufferedReader bfr;

    public static void main(String [] args){


        File file = new File("1.jpg");

        String suffixName = file.getName().substring(file.getName().indexOf(".")+1);
        System.out.println(suffixName);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String time = simpleDateFormat.format(new Date());
        //图片命名为time.suffixName

        String fileName = time+"."+suffixName;
        System.out.println(fileName);
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxMTExMTEiLCJleHAiOjE1NzgyMDU5Njl9.xty2Z9wTVZy9ROi9M18fz3Eg69Srahzv0GThe5ZJ9uM";
        String resp = postToUpload("http://139.224.192.113:8080/upload", file,fileName,token);
        System.out.println(resp);

    }

    private static String postToUpload(String url, File file,String fileName,String token) {
        try {
            URL ur = new URL(url);
            conn = (HttpURLConnection) ur.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            conn.setRequestProperty("Authorization", token);
            conn.setDoOutput(true);
            conn.setConnectTimeout(20 * 1000);
            conn.setReadTimeout(20 * 1000);
            conn.setDoInput(true);
            conn.connect();
            if (file != null) {
                DataOutputStream osw = new DataOutputStream(conn.getOutputStream());
                StringBuilder sb = new StringBuilder();
                sb.append(boundaryPrefix);
                sb.append(BOUNDARY);
                sb.append(newLine);
                sb.append("Content-Disposition: form-data; name=\"file\"; filename=\""+fileName+"\"" + "\r\n");
                sb.append("Content-Type: image/jpeg" + "\r\n");
                sb.append(newLine);
                osw.write(sb.toString().getBytes());
                DataInputStream in = new DataInputStream(new FileInputStream(file));
                byte[] b = new byte[1024];
                int len;
                while ((len = in.read(b)) != -1) {
                    osw.write(b, 0, len);
                }
                in.close();
                byte[] end_data = (newLine + boundaryPrefix + BOUNDARY
                        + boundaryPrefix + newLine).getBytes();
                osw.write(end_data);
                osw.flush();
                osw.close();

                int resCode = conn.getResponseCode();
                if (resCode == 200) {
                    InputStream i = conn.getInputStream();
                    bfr = new BufferedReader(new InputStreamReader(i));
                    String line;
                    String data = "";
                    while ((line = bfr.readLine()) != null) {
                        data = data + line;
                    }
                    bfr.close();
                    bfr = null;
                    return data;
                }
            }
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
